<div class="layui-form seller-form">
    <div class="layui-form-item">
        <div class="layui-inline">
            <div class="layui-input-inline">
                <button class="layui-btn layui-btn-sm add-rule"><i class="layui-icon">&#xe608;</i> 添加</button>
            </div>
        </div>
    </div>

</div>

<div class="table-body">
    <table id="ruleTable" lay-filter="ruleTable"></table>
</div>

<script>
    layui.config({
        base: '__STATIC_LIB__layuiadmin/layui/treeTable/'
    }).extend({
        treetable: 'treeTable'
    }).use(['layer', 'table', 'treeTable', 'form', 'jquery'], function () {
        var $ = layui.jquery,
            layer = layui.layer,
            table = layui.table,
            form = layui.form,
            treeTable = layui.treeTable;
        var renderTable = function () {
            treeTable.render({
                elem: '#ruleTable',
                tree: {
                    iconIndex: 1,  // 折叠图标显示在第几列
                    idName: 'id',  // 自定义id字段的名称
                    pidName: 'pid',  // 自定义标识是否还有子节点的字段名称
                    haveChildName: 'haveChild',  // 自定义标识是否还有子节点的字段名称
                    isPidData: true,  // 是否是pid形式数据
                    arrowType: 'arrow2',   // 自定义箭头风格
                    getIcon: function(d) {  // 自定义图标
                        // d是当前行的数据
                        if (d.haveChild) {  // 判断是否有子集
                            return '<i class="ew-tree-icon ew-tree-icon-folder"></i>';
                        } else {
                            return '<i class="ew-tree-icon ew-tree-icon-file"></i>';
                        }
                    }

                },
                cols: [
                    {type: 'numbers'},
                    {field: 'title', title: '规则名称'},
                    {field: 'name', title: 'url', width: 160},
                    {field: 'icon', title: '图标', width: 160},
                    {field: 'createTime', title: '创建时间', width: 180},
                    {
                        field: 'sort',
                        title: '排序',
                        align:'center',
                        width:100
                    }, {
                        align: 'center',
                        title: '操作',
                        toolbar: '#ruleBar'
                    }
                ],
                reqData: function(data, callback) {
                    // 在这里写ajax请求，通过callback方法回调数据
                    JsGet("{:url('rule/index')}", function (res) {
                        callback(res.data);  // 参数是数组类
                    })


                }
            });
        };
        renderTable();
        //添加规则
        $(document).on('click', '.add-rule', function () {
            JsGet("{:url('rule/add')}", function (e) {
                if (e.status) {
                    window.box = layer.open({
                        type: 1,
                        content: e.data,
                        area: ['80%', '80%'],
                        title: '添加规则',
                        btn: ['确定', '取消'],
                        zIndex: 1800,
                        yes: function () {
                            var data = $(".ruleForm").serializeArray();
                            console.log(data);
                            $.ajax({
                                type: 'post',
                                url: '{:url("rule/add")}',
                                data: data,
                                dataType: 'json',
                                success: function (e) {
                                    if (e.status) {
                                        layer.close(window.box);
                                        renderTable();
                                    } else {
                                        layer.msg(e.msg);
                                    }
                                }
                            });
                        }
                    });
                }else{
                    layer.msg(e.msg);
                }
            })
        });
        //监听工具条
        treeTable.on('tool(ruleTable)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if (layEvent === 'del') { //删除
                layer.confirm('真的要删除么', {icon: 3}, function (index) {
                    JsGet("{:url('rule/del')}?id=" + data.id, function (res) {
                        if (res.status) {
                            obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                            layer.close(index);//向服务端发送删除指令
                            renderTable();
                        }
                        layer.msg(res.msg);
                    })
                });
            } else if (layEvent === 'edit') { //编辑
                JsGet("{:url('rule/edit')}?id=" + data.id, function (e) {
                    if (e.status) {
                        window.box = layer.open({
                            type: 1,
                            content: e.data,
                            area: ['80%', '80%'],
                            title: '修改节点',
                            btn: ['确定', '取消'],
                            zIndex: 1800,
                            yes: function () {
                                var data = $("#ruleEdit").serializeArray();
                                $.ajax({
                                    type: 'post',
                                    url: '{:url("rule/edit")}',
                                    data: data,
                                    dataType: 'json',
                                    success: function (e) {
                                        if (e.status) {
                                            layer.msg(e.msg);
                                            layer.close(window.box);
                                            renderTable();
                                        } else {
                                            layer.msg(e.msg);
                                        }
                                    }
                                });
                            }
                        })
                    }else{
                        layer.msg(e.msg);
                    }
                })
            } else if (layEvent === 'add_type') {
                //添加子节点
                JsGet("{:url('rule/addSon')}?id=" + data.id, function (e) {
                    if (e.status) {
                        window.box = layer.open({
                            type: 1,
                            content: e.data,
                            area: ['400px', '300px'],
                            title: '添加节点',
                            btn: ['确定', '取消'],
                            zIndex: 1800,
                            yes: function () {
                                var data = $("#ruleAddson").serializeArray();
                                $.ajax({
                                    type: 'post',
                                    url: '{:url("rule/add")}',
                                    data: data,
                                    dataType: 'json',
                                    success: function (e) {
                                        if (e.status) {
                                            layer.msg(e.msg);
                                            layer.close(window.box);
                                            renderTable();
                                        } else {
                                            layer.msg(e.msg);
                                        }
                                    }
                                });
                            }
                        })
                    }else{
                        layer.msg(e.msg);
                    }
                })
            }
        });
    })
</script>

<script id="ruleBar" type="text/html">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add_type">添加子节点</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
